package com.epfresh.supplier.entity;

import com.epfresh.api.utils.FormatUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author wangzhanghuan
 * @version 1.0
 * @date create 17-12-18
 * @Description
 */

public class GrouponInfo {
//        "maxProductCountLimited": 50.0,
//        "agencyCityName": "上海市",

    private String maxProductCountLimited;
    private String minProductCountLimited;
    private String agencyCityName;
    private BuyerProduct product;

    private BaseStoreInfo baseStore;
    private List<BaseProductAreaConfigPrices> prices;


    String deliveryDate;

    String productCount;

    private AgencyListItem agent;

    public AgencyListItem getAgent() {
        return agent;
    }

    public void setAgent(AgencyListItem agent) {
        this.agent = agent;
    }

    private List<HashMap<String,String>> deliveryDateList;


    public BaseStoreInfo getBaseStore() {
        return baseStore;
    }

    public String getBaseStoreId() {
        if (baseStore != null)
            return baseStore.getId();
        return null;
    }

    public String getProductCount() {
        return productCount;
    }

    public void setProductCout(String productCout) {
        this.productCount = productCout;
    }

    public void setBaseStore(BaseStoreInfo baseStore) {
        this.baseStore = baseStore;
    }

    public String getMaxProductCountLimited() {
        return maxProductCountLimited;
    }

    public void setMaxProductCountLimited(String maxProductCountLimited) {
        this.maxProductCountLimited = maxProductCountLimited;
    }

    public String getMinProductCountLimited() {
        return minProductCountLimited;
    }

    public void setMinProductCountLimited(String minProductCountLimited) {
        this.minProductCountLimited = minProductCountLimited;
    }

    public String getAgencyCityName() {
        return agencyCityName;
    }

    public String getBaseCityName() {
        if (baseStore != null) {
            return baseStore.getCityName();
        }
        return "";
    }

    public String getChargeUnit() {
        if (product != null) {
            return product.getChargeUnit();
        }
        return "";
    }
    public String getFreightUnit() {
        if (product != null) {
            return product.getFreightUnit();
        }
        return "";
    }
    public String getDeposit() {
        if (product != null) {
            return product.getDeposit();
        }
        return "";
    }
    public String getDepositLast() {
        if (product != null) {
            return product.getDepositLast();
        }
        return "";
    }

    public void setAgencyCityName(String agencyCityName) {
        this.agencyCityName = agencyCityName;
    }

    public BuyerProduct getProduct() {
        return product;
    }

    public void setProduct(BuyerProduct product) {
        this.product = product;
    }

    public List<BaseProductAreaConfigPrices> getPrices() {
        return prices;
    }

    public void setPrices(List<BaseProductAreaConfigPrices> prices) {
        this.prices = prices;
    }

    public List<HashMap<String, String>> getDeliveryDateList() {
        return deliveryDateList;
    }

    public void setDeliveryDateList(List<HashMap<String, String>> deliveryDateList) {
        this.deliveryDateList = deliveryDateList;
    }


    public double getUnitPrice() {
        if (prices != null && prices.size() > 0) {

            double price = 0;
            int size = prices.size();
            for (int i = 0; i < size; i++) {

                BaseProductAreaConfigPrices bpa = prices.get(i);

                if (bpa != null) {
                    String minQuantity = bpa.getMinQuantity();
                    String maxQuantity = bpa.getMaxQuantity();

                    if (FormatUtil.countAdd(maxQuantity, productCount + "", false) >= 0 &&
                            FormatUtil.countAdd(productCount + "", minQuantity, false) >= 0) {

                        return bpa.getPrice();
                    }
                }
            }


            return price;

        }

        return 0;
    }

    public void init() {
        productCount = minProductCountLimited;
    }
    public String getDeliveryDate() {

        if (deliveryDate == null || "".equals(deliveryDate)) {
            if (deliveryDateList != null && deliveryDateList.size() > 0) {
                HashMap<String, String> map = deliveryDateList.get(0);
                if (map != null) {
                    String format2 = map.get("format2");
                    deliveryDate = format2;
                }

            }
        }

        return deliveryDate == null ? "" : deliveryDate;
    }

    public void updateDeliveryDate(int position) {
        if (deliveryDateList != null && position < deliveryDateList.size()) {
            HashMap<String, String> map = deliveryDateList.get(position);
            if (map != null) {
                String format1 = map.get("format2");
                if (format1 != null) {
                    deliveryDate = format1;
                }
            }

        }
    }
    public void updateDeliveryDate(String time) {
        if (deliveryDateList != null) {

            for (int i = 0 ;i<deliveryDateList.size();i++) {
                HashMap<String, String> map = deliveryDateList.get(i);
                if (map != null) {
                    String format1 = map.get("format1");
                    if (format1 != null && format1.equals(time)) {
                        deliveryDate = map.get("format2");
                    }
                }
            }

        }
    }

    public ArrayList<String> getDeliveryList() {
        ArrayList<String> timeStringList = new ArrayList<>();

        if (deliveryDateList != null && deliveryDateList.size() > 0) {
            int size = deliveryDateList.size();
            for (int i = 0; i < size; i++) {

                HashMap<String, String> map = deliveryDateList.get(i);
                if (map != null) {
                    String format1 = map.get("format1");
                    if (format1 != null) {
                        timeStringList.add(format1);
                    }
                }
            }

        }


        return timeStringList;
    }

    public int getDeliveryListIndex() {
        String deliveryDate = getDeliveryDate();

        if (deliveryDateList != null && deliveryDateList.size() > 0) {
            int size = deliveryDateList.size();
            for (int i = 0; i < size; i++) {

                HashMap<String, String> map = deliveryDateList.get(i);
                if (map != null) {
                    String format1 = map.get("format2");
                    if (format1 != null && format1.equals(deliveryDate)) {
                        return i;
                    }
                }
            }

        }


        return 0;
    }

    public String getDeliveryInfo() {

        if (product == null) {
            return "";
        }
        String deliveryDate = getDeliveryDate();
        return product.getDeliveryDuration();
    }
    public String getDeliveryInfoFull() {

        if (product == null) {
            return "";
        }
        String deliveryDate = getDeliveryDate();
        return "预计" + deliveryDate + "发货（约" + product.getDeliveryDuration() + "送达）";

    }

}
